﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DadaLib
{
    /// <summary>
    /// Specialized generator that accepts n generators and creates sentences jumping betwwen 
    /// generators in a round-robin way.
    /// </summary>
    public class RoundRobinGenerator : IGenerator
    {
        private List<Generator> generators;
        private int currentGeneratorIndex;

        /// <summary>
        /// Initializes a new instance of the <see cref="RoundRobinGenerator"/> class.
        /// </summary>
        /// <param name="generators">The generators.</param>
        public RoundRobinGenerator(List<Generator> generators)
        {
            this.generators = generators;
            currentGeneratorIndex = 0;
        }

        public void Analize(string fileName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Analizes the input texts from every generator.
        /// </summary>
        public void Analize()
        {
            generators.ForEach(g => g.Analize());
        }

        /// <summary>
        /// Gets the next word in the sentence.
        /// </summary>
        /// <param name="seedWord">The seed word.</param>
        /// <returns>The next word in sequence</returns>
        public string GetNextWord(string seedWord)
        {
            string word = null;
            do
            {
                currentGeneratorIndex = (currentGeneratorIndex + 1) % generators.Count;
                word = generators[currentGeneratorIndex].GetNextWord(seedWord);                
            } while (word == null);
            return word;
        }

        /// <summary>
        /// Searches for a word suitable for starting a sentence.
        /// </summary>
        /// <returns>A word</returns>
        public string SearchStartWord()
        {
            var word = generators[0].SearchStartWord();
            if (generators.Count > 1)
                currentGeneratorIndex++;
            return word;
        }
    }
}
